package com.jerry.base.io.in;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * User: Jerry
 * Date: 2024/1/22
 * Time: 22:10
 * Description:
 */
@Slf4j
public class FileInputStreamUsage {

    /**
     * 单个字节读取
     *
     * @throws IOException
     */
    @Test
    public void readFile() throws IOException {
        String filePath = "src/main/resources/io/dir/hello.txt";
        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            int readData;
            while ((readData = fileInputStream.read()) != -1) {
                System.out.print((char) readData);
            }
        }
    }

    /**
     * 多个字节读取
     *
     * @throws IOException
     */
    @Test
    public void readFile2() throws IOException {
        String filePath = "src/main/resources/io/dir/hello.txt";
        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            int length;
            byte[] bytes = new byte[8];
            while ((length = fileInputStream.read(bytes)) != -1) {
                System.out.print(new String(bytes, 0, length));
            }
        }
    }

}
